package com.cskaoyan.service;

import com.cskaoyan.bean.entity.AdminExample;
import com.cskaoyan.bean.entity.Admin;
import com.cskaoyan.bean.entity.AdminExample;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.common.util.ObjectUtils;
import com.cskaoyan.common.util.StringUtils;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.utils.MarketPageUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: TODO
 * @Author: GaoJia
 * @Date: 2021/10/18 21:10
 */
@Service
public class AdminServiceImpl implements  AdminService{

    @Autowired
    AdminMapper adminMapper;

    @Override
    public BasePageData query(String username, BaseParam param) {
        //配置分页信息 → 在执行预编译过程，会帮你拼接分页的sql语句
        MarketPageUtil.startPage(param);

        AdminExample example = new AdminExample();
        //构造条件 → List<Criterion> → and方法做的事情就是构造一个Criterion的对象添加到List里
        //如果 username 不为null，拼接条件 and username like '%%'
        AdminExample.Criteria criteria = example.createCriteria();
        if (ObjectUtils.isNotEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        //设置排序
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Admin> admins = adminMapper.selectByExample(example);

        return BasePageData.ok(admins);
    }

    @Override
    public BasePageData create(Admin admin) {
        return null;
    }

    @Override
    public int delete(Admin admin) {
        return adminMapper.deleteByPrimaryKey(admin.getId());
    }

    @Override
    public BasePageData update(Admin admin) {
        return null;
    }

}
